<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.14.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta property="og:type" content="website">
<meta property="og:title" content="JsyBlog">
<meta property="og:url" content="http://example.com/page/10/index.html">
<meta property="og:site_name" content="JsyBlog">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="SongyangJi">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/page/10/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":true,"isPost":false,"lang":"zh-CN","comments":"","permalink":"","path":"page/10/index.html","title":""}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>JsyBlog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">JsyBlog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">SongyangJi</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">251</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">45</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">109</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner index posts-expand">

    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/06/09/Docker%E5%AE%B9%E5%99%A8%E9%99%90%E5%88%B6CPU/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="undefined | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2022/06/09/Docker%E5%AE%B9%E5%99%A8%E9%99%90%E5%88%B6CPU/" class="post-title-link" itemprop="url">Docker容器限制CPU</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-06-09 11:11:04" itemprop="dateCreated datePublished" datetime="2022-06-09T11:11:04+08:00">2022-06-09</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-04-08 15:33:20" itemprop="dateModified" datetime="2023-04-08T15:33:20+08:00">2023-04-08</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Docker/" itemprop="url" rel="index"><span itemprop="name">Docker</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="压测工具"><a href="#压测工具" class="headerlink" title="压测工具"></a>压测工具</h1><p>使用ubuntu的<code>stress</code> 作为压测工具，并制作镜像</p>
<figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">FROM</span> ubuntu:latest</span><br><span class="line"></span><br><span class="line"><span class="keyword">RUN</span><span class="language-bash"> apt-get update &amp;&amp; \</span></span><br><span class="line"><span class="language-bash">        apt-get install stress</span></span><br></pre></td></tr></table></figure>
<p>shell命令</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker build -t u-stress:latest .</span><br></pre></td></tr></table></figure>



<h1 id="限制CPU资源"><a href="#限制CPU资源" class="headerlink" title="限制CPU资源"></a>限制CPU资源</h1><h2 id="限制CPU核心个数（百分比）"><a href="#限制CPU核心个数（百分比）" class="headerlink" title="限制CPU核心个数（百分比）"></a>限制CPU核心个数（百分比）</h2><p>在 docker 1.13 及更高的版本上，能够很容易的限制容器可以使用的主机 CPU 核心个数。<br>只需要通过 –cpus 选项指定容器可以使用的 CPU 个数就可以了，并且还可以指定如 1.5 之类的小数。（为什么还可以是小数呢，后面解释）</p>
<p>通过下面的命令创建容器，–cpus&#x3D;2 表示容器最多可以使用主机上两个 CPU：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it --rm --cpus=2 u-stress:latest /bin/bash</span><br></pre></td></tr></table></figure>
<p>然后由 stress 命令创建四个繁忙的进程消耗 CPU 资源：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">stress -c 4</span><br></pre></td></tr></table></figure>
<p>使用<code>docker stats</code>查看容状态，发现容器 CPU 的负载为 200%左右，它的含义为单个 CPU 负载的两倍。我们也可以把它理解为有两颗 CPU核心在 100% 的为它工作。</p>
<p>再让我们通过 top 命令看看主机 CPU 的真实负载情况：</p>
<p><img src="/2022/06/09/Docker%E5%AE%B9%E5%99%A8%E9%99%90%E5%88%B6CPU/Docker%E5%AE%B9%E5%99%A8%E9%99%90%E5%88%B6CPU-0.png" alt="img"></p>
<p>实际的情况并不是两个 CPU 负载 100%，而另外两个负载 0%。四个 CPU 的负载都是 50%，加起来容器消耗的 CPU 总量就是两个 CPU 100% 的负载。</p>
<p><strong>所以对于进程来说是没有 CPU 个数这一概念的</strong>，内核只能通过进程消耗的 CPU 时间片来统计出进程占用 CPU 的百分比。这也是我们看到的各种工具中都使用百分比来说明 CPU 使用率的原因。<br>并且 –cpus 选项支持设为小数也从侧面说明了对 CPU 的计量只能是百分比。</p>
<h2 id="指定固定的-CPU集"><a href="#指定固定的-CPU集" class="headerlink" title="指定固定的 CPU集"></a>指定固定的 CPU集</h2><p>通过 –cpus 选项我们无法让容器始终在一个或某几个 CPU 上运行，但是通过<code>--cpuset-cpus</code> 选项却可以做到！这是非常有意义的，因为现在的多核系统中每个核心都有自己的缓存，如果频繁的调度进程在不同的核心上执行势必会带来缓存失效等开销。下面我们就演示如何设置容器使用固定的 CPU，下面的命令为容器设置了 –cpuset-cpus 选项，指定运行容器的 CPU 编号为 1：(编号从0开始)</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it --rm --cpuset-cpus=&quot;1&quot; u-stress:latest /bin/bash</span><br></pre></td></tr></table></figure>



<p>再启动压力测试命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">stress -c 4</span><br></pre></td></tr></table></figure>

<p>然后查看主机 CPU 的负载情况：</p>
<p><img src="/2022/06/09/Docker%E5%AE%B9%E5%99%A8%E9%99%90%E5%88%B6CPU/Docker%E5%AE%B9%E5%99%A8%E9%99%90%E5%88%B6CPU-1.png" alt="img"></p>
<p>这次只有 Cpu1 达到了 100%，其它的 CPU 并未被容器使用。我们还可以反复的执行 stress -c 4 命令，但是始终都是 Cpu1 在干活。<br>再看看容器的 CPU 负载，也是只有 100%：</p>
<p><img src="/2022/06/09/Docker%E5%AE%B9%E5%99%A8%E9%99%90%E5%88%B6CPU/Docker%E5%AE%B9%E5%99%A8%E9%99%90%E5%88%B6CPU-2.png"></p>
<p>-cpuset-cpus 选项还可以一次指定多个 CPU：<br>比如：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it --rm --cpuset-cpus=&quot;1,3&quot; u-stress:latest /bin/bash</span><br></pre></td></tr></table></figure>
<p>–cpuset-cpus 选项的一个缺点是必须指定 CPU 在操作系统中的编号，这对于动态调度的环境(无法预测容器会在哪些主机上运行，只能通过程序动态的检测系统中的 CPU 编号，并生成 docker run 命令)会带来一些不便。</p>
<h2 id="设置使用-CPU-的权重"><a href="#设置使用-CPU-的权重" class="headerlink" title="设置使用 CPU 的权重"></a>设置使用 CPU 的权重</h2><p>当 CPU 资源充足时，设置 CPU 的权重是没有意义的。只有在容器争用 CPU 资源的情况下， CPU 的权重才能让不同的容器分到不同的 CPU 用量。–cpu-shares 选项用来设置 CPU 权重，它的默认值为 1024。我们可以把它设置为 2 表示很低的权重，但是设置为 0 表示使用默认值 1024。<br>下面我们分别运行两个容器，指定它们都使用 Cpu0，并分别设置 –cpu-shares 为 512 和 1024：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">docker run -it --<span class="built_in">rm</span> --cpuset-cpus=<span class="string">&quot;0&quot;</span> --cpu-shares=512 u-stress:latest /bin/bash</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">docker run -it --<span class="built_in">rm</span> --cpuset-cpus=<span class="string">&quot;0&quot;</span> --cpu-shares=1024 u-stress:latest /bin/bash</span></span><br></pre></td></tr></table></figure>

<p>在两个容器中都运行 stress -c 4 命令。</p>
<p>此时主机 Cpu0 的负载为 100%：</p>
<p><img src="/2022/06/09/Docker%E5%AE%B9%E5%99%A8%E9%99%90%E5%88%B6CPU/Docker%E5%AE%B9%E5%99%A8%E9%99%90%E5%88%B6CPU-3.png"></p>
<p>容器中 CPU 的负载为：</p>
<p><img src="/2022/06/09/Docker%E5%AE%B9%E5%99%A8%E9%99%90%E5%88%B6CPU/Docker%E5%AE%B9%E5%99%A8%E9%99%90%E5%88%B6CPU-4.png"></p>
<p>两个容器分享一个 CPU，所以总量应该是 100%。具体每个容器分得的负载则取决于 –cpu-shares 选项的设置！我们的设置分别是 512 和 1024，则它们分得的比例为 1:2。在本例中如果想让两个容器各占 50%，只要把 –cpu-shares 选项设为相同的值就可以了。</p>
<p>参考：</p>
<p><a target="_blank" rel="noopener" href="https://sanhe-dev-exp-share.readthedocs.io/03-Docker/03-Restrict-CPU-Memory-and-GPU/README.html">https://sanhe-dev-exp-share.readthedocs.io/03-Docker/03-Restrict-CPU-Memory-and-GPU/README.html</a></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/05/28/%E5%A4%9A%E7%A7%8D%E6%96%B9%E5%BC%8F%E8%A7%A6%E5%8F%91Jenkins%E6%B5%81%E6%B0%B4%E7%BA%BF/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="undefined | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2022/05/28/%E5%A4%9A%E7%A7%8D%E6%96%B9%E5%BC%8F%E8%A7%A6%E5%8F%91Jenkins%E6%B5%81%E6%B0%B4%E7%BA%BF/" class="post-title-link" itemprop="url">多种方式触发Jenkins流水线</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2022-05-28 12:12:34 / 修改时间：12:13:55" itemprop="dateCreated datePublished" datetime="2022-05-28T12:12:34+08:00">2022-05-28</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Jenkins/" itemprop="url" rel="index"><span itemprop="name">Jenkins</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h3 id="代码来源"><a href="#代码来源" class="headerlink" title="代码来源"></a>代码来源</h3><h3 id="源项目"><a href="#源项目" class="headerlink" title="源项目"></a>源项目</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl $&#123;src_url&#125; -o $&#123;service_id&#125;.tar.gz &amp;&amp; mkdir $&#123;service_id&#125; &amp;&amp; tar -xzf $&#123;service_id&#125;.tar.gz -C $&#123;service_id&#125; &amp;&amp; rm $&#123;service_id&#125;.tar.gz</span><br></pre></td></tr></table></figure>


      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/05/19/MacOS%E5%AE%89%E8%A3%85Jenkins/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="undefined | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2022/05/19/MacOS%E5%AE%89%E8%A3%85Jenkins/" class="post-title-link" itemprop="url">MacOS安装Jenkins</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2022-05-19 15:53:36 / 修改时间：17:45:44" itemprop="dateCreated datePublished" datetime="2022-05-19T15:53:36+08:00">2022-05-19</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Jenkins/" itemprop="url" rel="index"><span itemprop="name">Jenkins</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p>Mac用户使用brew安装jenkins最为方便了</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">安装</span></span><br><span class="line">brew update</span><br><span class="line">brew install jenkins</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">更新</span></span><br><span class="line">brew upgrade jenkins</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">启动</span></span><br><span class="line">brew services start jenkins</span><br></pre></td></tr></table></figure>



<p>查看admin密码</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cat .jenkins/secrets/initialAdminPassword</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">a5f8fbee0dfe4fec807b71ae42a1a1f8</span></span><br></pre></td></tr></table></figure>


      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/05/18/CSRF/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="undefined | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2022/05/18/CSRF/" class="post-title-link" itemprop="url">CSRF</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-05-18 04:22:51" itemprop="dateCreated datePublished" datetime="2022-05-18T04:22:51+08:00">2022-05-18</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/05/16/Docker%E5%AE%B9%E5%99%A8%E5%81%A5%E5%BA%B7%E6%A3%80%E6%9F%A5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="undefined | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2022/05/16/Docker%E5%AE%B9%E5%99%A8%E5%81%A5%E5%BA%B7%E6%A3%80%E6%9F%A5/" class="post-title-link" itemprop="url">Docker容器健康检查</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-05-16 10:11:25" itemprop="dateCreated datePublished" datetime="2022-05-16T10:11:25+08:00">2022-05-16</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-05-17 16:11:50" itemprop="dateModified" datetime="2022-05-17T16:11:50+08:00">2022-05-17</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Docker/" itemprop="url" rel="index"><span itemprop="name">Docker</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h3 id="结构体定义"><a href="#结构体定义" class="headerlink" title="结构体定义"></a>结构体定义</h3><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Health states</span></span><br><span class="line">const (</span><br><span class="line">   NoHealthcheck = <span class="string">&quot;none&quot;</span>      <span class="comment">// Indicates there is no healthcheck</span></span><br><span class="line">   Starting      = <span class="string">&quot;starting&quot;</span>  <span class="comment">// Starting indicates that the container is not yet ready</span></span><br><span class="line">   Healthy       = <span class="string">&quot;healthy&quot;</span>   <span class="comment">// Healthy indicates that the container is running correctly</span></span><br><span class="line">   Unhealthy     = <span class="string">&quot;unhealthy&quot;</span> <span class="comment">// Unhealthy indicates that the container has a problem</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">// Health stores information about the container&#x27;s healthcheck results</span></span><br><span class="line">type Health struct <span class="punctuation">&#123;</span></span><br><span class="line">   Status        string               <span class="comment">// Status is one of Starting, Healthy or Unhealthy</span></span><br><span class="line">   FailingStreak int                  <span class="comment">// FailingStreak is the number of consecutive failures</span></span><br><span class="line">   Log           <span class="punctuation">[</span><span class="punctuation">]</span>*HealthcheckResult <span class="comment">// Log contains the last few results (oldest first)</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// ContainerState stores container&#x27;s running state</span></span><br><span class="line"><span class="comment">// it&#x27;s part of ContainerJSONBase and will return by &quot;inspect&quot; command</span></span><br><span class="line">type ContainerState struct <span class="punctuation">&#123;</span></span><br><span class="line">   Status     string <span class="comment">// String representation of the container state. Can be one of &quot;created&quot;, &quot;running&quot;, &quot;paused&quot;, &quot;restarting&quot;, &quot;removing&quot;, &quot;exited&quot;, or &quot;dead&quot;</span></span><br><span class="line">   Running    bool</span><br><span class="line">   Paused     bool</span><br><span class="line">   Restarting bool</span><br><span class="line">   OOMKilled  bool</span><br><span class="line">   Dead       bool</span><br><span class="line">   Pid        int</span><br><span class="line">   ExitCode   int</span><br><span class="line">   Error      string</span><br><span class="line">   StartedAt  string</span><br><span class="line">   FinishedAt string</span><br><span class="line">   Health     *Health `json<span class="punctuation">:</span><span class="string">&quot;,omitempty&quot;</span>`</span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>



<h3 id="容器状态举例"><a href="#容器状态举例" class="headerlink" title="容器状态举例"></a>容器状态举例</h3><h4 id="正在运行、有健康检查"><a href="#正在运行、有健康检查" class="headerlink" title="正在运行、有健康检查"></a>正在运行、有健康检查</h4><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">[</span></span><br><span class="line">    <span class="punctuation">&#123;</span></span><br><span class="line">        <span class="attr">&quot;Id&quot;</span><span class="punctuation">:</span> <span class="string">&quot;62b16c0d39e0d53cb9b650abbec981c6a05487b44ca5a3e0a70add7ab0b65cba&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;State&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">            <span class="attr">&quot;Status&quot;</span><span class="punctuation">:</span> <span class="string">&quot;running&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Running&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Paused&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Restarting&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;OOMKilled&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Dead&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Pid&quot;</span><span class="punctuation">:</span> <span class="number">667960</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;ExitCode&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Error&quot;</span><span class="punctuation">:</span> <span class="string">&quot;&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;StartedAt&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-05-14T20:38:38.295539629Z&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;FinishedAt&quot;</span><span class="punctuation">:</span> <span class="string">&quot;0001-01-01T00:00:00Z&quot;</span></span><br><span class="line">        <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>



<h4 id="正在运行、无健康检查"><a href="#正在运行、无健康检查" class="headerlink" title="正在运行、无健康检查"></a>正在运行、无健康检查</h4><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">[</span></span><br><span class="line">    <span class="punctuation">&#123;</span></span><br><span class="line">        <span class="attr">&quot;Id&quot;</span><span class="punctuation">:</span> <span class="string">&quot;d8338185a6bfc54dca030c59abb5186161bf77094c1af8979c58b93540599905&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;State&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">            <span class="attr">&quot;Status&quot;</span><span class="punctuation">:</span> <span class="string">&quot;running&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Running&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Paused&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Restarting&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;OOMKilled&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Dead&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Pid&quot;</span><span class="punctuation">:</span> <span class="number">458280</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;ExitCode&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Error&quot;</span><span class="punctuation">:</span> <span class="string">&quot;&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;StartedAt&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-05-14T13:02:57.171746945Z&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;FinishedAt&quot;</span><span class="punctuation">:</span> <span class="string">&quot;0001-01-01T00:00:00Z&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Health&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">                <span class="attr">&quot;Status&quot;</span><span class="punctuation">:</span> <span class="string">&quot;healthy&quot;</span><span class="punctuation">,</span></span><br><span class="line">                <span class="attr">&quot;FailingStreak&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">                <span class="comment">// output 为日志的输出，这里省略</span></span><br><span class="line">                <span class="attr">&quot;Log&quot;</span><span class="punctuation">:</span> <span class="punctuation">[</span></span><br><span class="line">                    <span class="punctuation">&#123;</span></span><br><span class="line">                        <span class="attr">&quot;Start&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-05-16T10:20:11.364683294+08:00&quot;</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;End&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-05-16T10:20:11.489168017+08:00&quot;</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;ExitCode&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;Output&quot;</span><span class="punctuation">:</span> <span class="string">&quot;&quot;</span></span><br><span class="line">                    <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">                    <span class="punctuation">&#123;</span></span><br><span class="line">                        <span class="attr">&quot;Start&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-05-16T10:20:41.555034247+08:00&quot;</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;End&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-05-16T10:20:41.669428317+08:00&quot;</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;ExitCode&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;Output&quot;</span><span class="punctuation">:</span> <span class="string">&quot;&quot;</span></span><br><span class="line">                    <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">                    <span class="punctuation">&#123;</span></span><br><span class="line">                        <span class="attr">&quot;Start&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-05-16T10:21:11.743222508+08:00&quot;</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;End&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-05-16T10:21:11.873154539+08:00&quot;</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;ExitCode&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;Output&quot;</span><span class="punctuation">:</span> <span class="string">&quot;&quot;</span></span><br><span class="line">                    <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">                    <span class="punctuation">&#123;</span></span><br><span class="line">                        <span class="attr">&quot;Start&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-05-16T10:21:41.942537086+08:00&quot;</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;End&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-05-16T10:21:42.074005305+08:00&quot;</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;ExitCode&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;Output&quot;</span><span class="punctuation">:</span> <span class="string">&quot;&quot;</span></span><br><span class="line">                    <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">                    <span class="punctuation">&#123;</span></span><br><span class="line">                        <span class="attr">&quot;Start&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-05-16T10:22:12.145717622+08:00&quot;</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;End&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-05-16T10:22:12.298673054+08:00&quot;</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;ExitCode&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">                        <span class="attr">&quot;Output&quot;</span><span class="punctuation">:</span> <span class="string">&quot;&quot;</span></span><br><span class="line">                    <span class="punctuation">&#125;</span></span><br><span class="line">                <span class="punctuation">]</span></span><br><span class="line">            <span class="punctuation">&#125;</span></span><br><span class="line">        <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    <span class="punctuation">&#125;</span>  </span><br><span class="line"><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>



<h4 id="正常退出"><a href="#正常退出" class="headerlink" title="正常退出"></a>正常退出</h4><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">[</span></span><br><span class="line">    <span class="punctuation">&#123;</span></span><br><span class="line">        <span class="attr">&quot;Id&quot;</span><span class="punctuation">:</span> <span class="string">&quot;e6c371ee6eff329e7fea4411b3fe1f7374f155c89f0defe40469d24fe4966c84&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;State&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">            <span class="attr">&quot;Status&quot;</span><span class="punctuation">:</span> <span class="string">&quot;exited&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Running&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Paused&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Restarting&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;OOMKilled&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Dead&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Pid&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;ExitCode&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Error&quot;</span><span class="punctuation">:</span> <span class="string">&quot;&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;StartedAt&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-04-05T09:35:31.758578263Z&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;FinishedAt&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-04-05T09:35:31.75743118Z&quot;</span></span><br><span class="line">        <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">]</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h4 id="非正常退出"><a href="#非正常退出" class="headerlink" title="非正常退出"></a>非正常退出</h4><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">[</span></span><br><span class="line">    <span class="punctuation">&#123;</span></span><br><span class="line">        <span class="attr">&quot;Id&quot;</span><span class="punctuation">:</span> <span class="string">&quot;9960fb71ac67b3d0869ba699d722c1c2b1513e257bfa661d4357f3d0cc80712e&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;State&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">            <span class="attr">&quot;Status&quot;</span><span class="punctuation">:</span> <span class="string">&quot;exited&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Running&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Paused&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Restarting&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;OOMKilled&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Dead&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Pid&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;ExitCode&quot;</span><span class="punctuation">:</span> <span class="number">255</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;Error&quot;</span><span class="punctuation">:</span> <span class="string">&quot;&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;StartedAt&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-04-05T20:12:33.193708879Z&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;FinishedAt&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2022-05-07T18:10:37.476409625Z&quot;</span></span><br><span class="line">        <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>





<h2 id="实操"><a href="#实操" class="headerlink" title="实操"></a>实操</h2><h3 id="一个简单的健康检测"><a href="#一个简单的健康检测" class="headerlink" title="一个简单的健康检测"></a>一个简单的健康检测</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">hasHealthCheck</span><span class="params">(ctx context.Context, containerID <span class="type">string</span>)</span></span> (enabled <span class="type">bool</span>) &#123;</span><br><span class="line">	containerJSON, err := cli.ContainerInspect(ctx, containerID)</span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		log.Println(<span class="string">&quot;Error: [hasHealthCheck]&quot;</span>)</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> containerJSON.Config != <span class="literal">nil</span> &#123;</span><br><span class="line">		<span class="keyword">if</span> containerJSON.Config.Healthcheck != <span class="literal">nil</span> &#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">checkIsRunning</span><span class="params">(ctx context.Context, containerID <span class="type">string</span>)</span></span> <span class="type">bool</span> &#123;</span><br><span class="line">	containerJSON, err := cli.ContainerInspect(ctx, containerID)</span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		log.Printf(<span class="string">&quot;Error: [checkIsRunning] fail, err=%v&quot;</span>, err.Error())</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> containerJSON.State != <span class="literal">nil</span> &#123;</span><br><span class="line">		<span class="keyword">return</span> containerJSON.State.Running</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//No such container</span></span><br><span class="line">	<span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">MonitorContainerHealth</span><span class="params">(ctx context.Context, containerID <span class="type">string</span>)</span></span> &#123;</span><br><span class="line"></span><br><span class="line">	containerList, err := cli.ContainerList(ctx, types.ContainerListOptions&#123;All: <span class="literal">true</span>&#125;)</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		log.Printf(<span class="string">&quot;[MonitorContainerHealth] error=%v&quot;</span>, err)</span><br><span class="line">		os.Exit(<span class="number">1</span>)</span><br><span class="line">	&#125;</span><br><span class="line">	containerExists := <span class="literal">false</span></span><br><span class="line">	<span class="keyword">for</span> _, container := <span class="keyword">range</span> containerList &#123;</span><br><span class="line">		<span class="keyword">if</span> container.ID == containerID &#123;</span><br><span class="line">			containerExists = <span class="literal">true</span></span><br><span class="line">			<span class="keyword">break</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> !containerExists &#123;</span><br><span class="line">		log.Printf(<span class="string">&quot;[MonitorContainerHealth] container:%s not exists&quot;</span>, containerID)</span><br><span class="line">		os.Exit(<span class="number">1</span>)</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">var</span> checkContainerIsHealthy <span class="function"><span class="keyword">func</span><span class="params">(ctx context.Context, containerID <span class="type">string</span>)</span></span> <span class="type">bool</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> hasHealthCheck(ctx, containerID) &#123;</span><br><span class="line">		checkContainerIsHealthy = checkIsRunning <span class="comment">// todo 默认</span></span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		checkContainerIsHealthy = checkIsRunning</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">go</span> <span class="function"><span class="keyword">func</span><span class="params">(containerIsHealthy <span class="keyword">func</span>(ctx context.Context, containerID <span class="type">string</span>)</span></span> <span class="type">bool</span>) &#123;</span><br><span class="line">		<span class="keyword">for</span> &#123;</span><br><span class="line">			<span class="keyword">if</span> !containerIsHealthy(ctx, containerID) &#123;</span><br><span class="line">				log.Printf(<span class="string">&quot;[MonitorContainerHealth] container:%s is not running, so the monitor process terminate...&quot;</span>, containerID)</span><br><span class="line">				os.Exit(<span class="number">0</span>)</span><br><span class="line">			&#125;</span><br><span class="line">			time.Sleep(time.Second) <span class="comment">// todo default 1s</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;(checkContainerIsHealthy)</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/05/14/Homebrew%E6%9B%B4%E6%8D%A2%E9%95%9C%E5%83%8F%E6%BA%90/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="undefined | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2022/05/14/Homebrew%E6%9B%B4%E6%8D%A2%E9%95%9C%E5%83%8F%E6%BA%90/" class="post-title-link" itemprop="url">Homebrew更换镜像源</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-05-14 02:59:24" itemprop="dateCreated datePublished" datetime="2022-05-14T02:59:24+08:00">2022-05-14</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-02-26 02:23:46" itemprop="dateModified" datetime="2023-02-26T02:23:46+08:00">2023-02-26</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Tools/" itemprop="url" rel="index"><span itemprop="name">Tools</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="替换brew-git"><a href="#替换brew-git" class="headerlink" title="替换brew.git"></a>替换brew.git</h1><p>cd “$(brew –repo)”<br>git remote set-url origin <a target="_blank" rel="noopener" href="https://mirrors.aliyun.com/homebrew/brew.git">https://mirrors.aliyun.com/homebrew/brew.git</a></p>
<h1 id="替换homebrew-core-git"><a href="#替换homebrew-core-git" class="headerlink" title="替换homebrew-core.git"></a>替换homebrew-core.git</h1><p>cd “$(brew –repo)&#x2F;Library&#x2F;Taps&#x2F;homebrew&#x2F;homebrew-core”<br>git remote set-url origin <a target="_blank" rel="noopener" href="https://mirrors.aliyun.com/homebrew/homebrew-core.git">https://mirrors.aliyun.com/homebrew/homebrew-core.git</a></p>
<h1 id="替换homebrew-bottles访问地址"><a href="#替换homebrew-bottles访问地址" class="headerlink" title="替换homebrew-bottles访问地址"></a>替换homebrew-bottles访问地址</h1><p>echo ‘export HOMEBREW_BOTTLE_DOMAIN&#x3D;<a target="_blank" rel="noopener" href="https://mirrors.aliyun.com/homebrew/homebrew-bottles">https://mirrors.aliyun.com/homebrew/homebrew-bottles</a>‘ &gt;&gt; ~&#x2F;.zprofile<br>source ~&#x2F;.zprofile</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/05/13/Jenkins-API/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="undefined | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2022/05/13/Jenkins-API/" class="post-title-link" itemprop="url">Jenkins-API</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-05-13 02:43:18" itemprop="dateCreated datePublished" datetime="2022-05-13T02:43:18+08:00">2022-05-13</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-06-07 15:20:39" itemprop="dateModified" datetime="2022-06-07T15:20:39+08:00">2022-06-07</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Jenkins/" itemprop="url" rel="index"><span itemprop="name">Jenkins</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="REST-API"><a href="#REST-API" class="headerlink" title="REST API"></a>REST API</h1><p>Many objects of Jenkins provide the remote access API. They are available at <code>/.../api/</code> where “…” portion is the object for which you’d like to access.</p>
<ul>
<li><p><a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/xml">XML API</a></p>
<p>Access data exposed in <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/">HTML</a> as XML for machine consumption. <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/schema">Schema</a> is also available.You can also specify optional XPath to control the fragment you’d like to obtain (but see <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/#tree">below</a>). For example, <code>../api/xml?xpath=/*/*[0]</code>.For XPath that matches multiple nodes, you need to also specify the “wrapper” query parameter to specify the name of the root XML element to be create so that the resulting XML becomes well-formed.Similarly <code>exclude</code> query parameter can be used to exclude nodes that match the given XPath from the result. This is useful for trimming down the amount of data you fetch (but again see <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/#tree">below</a>). This query parameter can be specified multiple times.XPath filtering is powerful, and you can have it only return a very small data, but note that the server still has to build a full DOM of the raw data, which could cause a large memory spike. To avoid overloading the server, consider using the <code>tree</code> parameter, or use the <code>xpath</code> parameter in conjunction with the <code>tree</code> parameter. When used together, the result of the <code>tree</code> parameter filtering is built into DOM, then the XPath is applied to compute the final return value. In this way, you can often substantially reduce the size of DOM built in memory.</p>
</li>
<li><p><a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/json?pretty=true">JSON API</a></p>
<p>Access the same data as JSON for JavaScript-based access. <code>tree</code> may be used.</p>
</li>
<li><p><a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/python?pretty=true">Python API</a></p>
<p>Access the same data as Python for Python clients. This can be parsed into Python objects as <code>ast.literal_eval(urllib.urlopen(&quot;...&quot;).read())</code> and the resulting object tree is identical to that of JSON.</p>
</li>
</ul>
<p>For more information about remote API in Jenkins, see <a target="_blank" rel="noopener" href="https://www.jenkins.io/redirect/remote-api">the documentation</a>.</p>
<h2 id="Controlling-the-amount-of-data-you-fetch"><a href="#Controlling-the-amount-of-data-you-fetch" class="headerlink" title="Controlling the amount of data you fetch"></a>Controlling the amount of data you fetch</h2><p>The <code>tree</code> query parameter allows you to explicitly specify and retrieve only the information you are looking for, by using an XPath-ish path expression. The value should be a list of property names to include, with sub-properties inside square braces. Try [tree&#x3D;jobs[name],views[name,jobs[name]]](<a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/xml?tree=jobs%5Bname%5D,views%5Bname,jobs%5Bname%5D%5D">http://211.87.224.233:18080/api/xml?tree=jobs[name],views[name,jobs[name]]</a>) to see just a list of jobs (only giving the name) and views (giving the name and jobs they contain). <strong>Note</strong>: for array-type properties (such as <code>jobs</code> in this example), the name must be given in the original plural, not in the singular as the element would appear in XML (<code>&lt;job&gt;</code>). This will be more natural for e.g. [json?tree&#x3D;jobs<a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/json?tree=jobs%5Bname%5D">name]</a> anyway: the JSON writer does not do plural-to-singular mangling because arrays are represented explicitly.</p>
<p>For array-type properties, a range specifier is supported. For example, <code>tree=jobs[name]&#123;0,10&#125;</code> would retrieve the name of the first 10 jobs. The range specifier has the following variants:</p>
<ul>
<li><strong>{M,N}</strong>: From the M-th element (inclusive) to the N-th element (exclusive).</li>
<li><strong>{M,}</strong>: From the M-th element (inclusive) to the end.</li>
<li><strong>{,N}</strong>: From the first element (inclusive) to the N-th element (exclusive). The same as {0,N}.</li>
<li><strong>{N}</strong>: Just retrieve the N-th element. The same as {N,N+1}.</li>
</ul>
<p>Another way to retrieve more data is to use the <code>depth=N</code> query parameter. This retrieves all the data up to the specified depth. Compare <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/xml">depth&#x3D;0</a> and <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/xml?depth=1">depth&#x3D;1</a> and see what the difference is for yourself. Also note that data created by a smaller depth value is always a subset of the data created by a bigger depth value.</p>
<p>Because of the size of the data, the <code>depth</code> parameter should really be only used to explore what data Jenkins can return. Once you identify the data you want to retrieve, you can then come up with the <code>tree</code> parameter to exactly specify the data you need.</p>
<h2 id="Create-Job"><a href="#Create-Job" class="headerlink" title="Create Job"></a>Create Job</h2><p>要创建新的Job，请使用查询参数发布config.xml到此URL（http:&#x2F;&#x2F;{jenkinsUrl}&#x2F;createItem ），带上请求参数<code>name=*JOBNAME*</code>，请求头上带着<code>Content-Type: application/xml</code> 。</p>
<p>如果创建成功，您将获得 200 状态码，如果创建失败，您将获得 4xx&#x2F;5xx 码。</p>
<p><code>config.xml</code>是 Jenkins 用于将Job（无论是FreeStyle、Pipeline，还是其他类型的）存储在文件系统中的文件格式，因此可以在 Jenkins 主目录中查看它们的示例，或者通过直接从<code>/job/*JOBNAME*/config.xml</code>查询已经存在的job的xml定义。</p>
<h2 id="Copy-Job"><a href="#Copy-Job" class="headerlink" title="Copy Job"></a>Copy Job</h2><p>为了复制一个已有的Job，发送Post请求到此URL（http:&#x2F;&#x2F;{jenkinsUrl}&#x2F;createItem ），带上请求参数<code>name=*NEWJOBNAME*&amp;mode=copy&amp;from=*FROMJOBNAME*</code>。</p>
<h2 id="Build-Queue"><a href="#Build-Queue" class="headerlink" title="Build Queue"></a>Build Queue</h2><p>Build queue has <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/queue/api/">its own separate API</a>.</p>
<h2 id="Load-Statistics"><a href="#Load-Statistics" class="headerlink" title="Load Statistics"></a>Load Statistics</h2><p>Overall load statistics of Jenkins has <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/overallLoad/api/">its own separate API</a>.</p>
<h2 id="Restarting-Jenkins"><a href="#Restarting-Jenkins" class="headerlink" title="Restarting Jenkins"></a>Restarting Jenkins</h2><p>Jenkins will enter into the “quiet down” mode by sending a POST request with optional <code>reason</code> query parameter to <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/quietDown">this URL</a>. You can also send another request to this URL to update the reason. You can cancel this mode by sending a POST request to <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/cancelQuietDown">this URL</a>. On environments where Jenkins can restart itself (such as when Jenkins is installed as a Windows service), POSTing to <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/restart">this URL</a> will start the restart sequence, or <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/safeRestart">this URL</a> to restart once no jobs are running. All these URLs need Overall&#x2F;Manage permission (typically granted via Overall&#x2F;Administer).</p>
<blockquote>
<p>参考文档</p>
<p><a target="_blank" rel="noopener" href="https://ci.jenkins.io/api/">https://ci.jenkins.io/api/</a></p>
<p><a target="_blank" rel="noopener" href="https://www.jenkins.io/doc/book/using/remote-access-api/">https://www.jenkins.io/doc/book/using/remote-access-api/</a></p>
</blockquote>
<h1 id="使用-Jenkins-API的Java客户端"><a href="#使用-Jenkins-API的Java客户端" class="headerlink" title="使用 Jenkins API的Java客户端"></a>使用 Jenkins API的Java客户端</h1><h2 id="Maven依赖"><a href="#Maven依赖" class="headerlink" title="Maven依赖"></a>Maven依赖</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--jenkins-java-client--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.offbytwo.jenkins<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jenkins-client<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.3.8<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="连接Jenkins"><a href="#连接Jenkins" class="headerlink" title="连接Jenkins"></a>连接Jenkins</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.sdu.jsy.learnjenkins.config;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: SongyangJi</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@since</span>: 2022/5/17</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">@ConfigurationProperties(value = &quot;jenkins&quot;)</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@Getter</span></span><br><span class="line"><span class="meta">@Setter</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JenkinsConfig</span> &#123;</span><br><span class="line"></span><br><span class="line">    String url;</span><br><span class="line">    String username;</span><br><span class="line">    String token;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * JenkinsHttpClient 和 JenkinsServer 关系：</span></span><br><span class="line"><span class="comment">     * JenkinsServer 是对 JenkinsHttpClient某些功能的更高层次的封装，</span></span><br><span class="line"><span class="comment">     * JenkinsServer依赖于JenkinsHttpClient</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Http 客户端工具</span></span><br><span class="line"><span class="comment">     * 如果有些 API 该工具包未提供，可以用此Http客户端操作远程接口，执行命令</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="keyword">public</span> JenkinsHttpClient <span class="title function_">getHttpClient</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">JenkinsHttpClient</span> <span class="variable">jenkinsHttpClient</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            jenkinsHttpClient = <span class="keyword">new</span> <span class="title class_">JenkinsHttpClient</span>(<span class="keyword">new</span> <span class="title class_">URI</span>(url), username, token);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (URISyntaxException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> jenkinsHttpClient;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 连接 Jenkins</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="keyword">public</span> JenkinsServer <span class="title function_">connectServer</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">JenkinsServer</span> <span class="variable">jenkinsServer</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            jenkinsServer = <span class="keyword">new</span> <span class="title class_">JenkinsServer</span>(<span class="keyword">new</span> <span class="title class_">URI</span>(url), username, token);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (URISyntaxException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> jenkinsServer;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>






<h2 id="操作Job"><a href="#操作Job" class="headerlink" title="操作Job"></a>操作Job</h2><p>已有一条工作流：<a target="_blank" rel="noopener" href="http://localhost:8080/job/pipeline-test/">http://localhost:8080/job/pipeline-test/</a></p>
<h3 id="创建Job"><a href="#创建Job" class="headerlink" title="创建Job"></a>创建Job</h3><h4 id="查看xml定义"><a href="#查看xml定义" class="headerlink" title="查看xml定义"></a>查看xml定义</h4><p>查看定义它的xml：<a target="_blank" rel="noopener" href="http://localhost:8080/job/pipeline-test/config.xml">http://localhost:8080/job/pipeline-test/config.xml</a></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&#x27;1.1&#x27; encoding=&#x27;UTF-8&#x27;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">flow-definition</span> <span class="attr">plugin</span>=<span class="string">&quot;workflow-job@1180.v04c4e75dce43&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">actions</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction</span> <span class="attr">plugin</span>=<span class="string">&quot;pipeline-model-definition@2.2077.vc78ec45162f1&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction</span> <span class="attr">plugin</span>=<span class="string">&quot;pipeline-model-definition@2.2077.vc78ec45162f1&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">jobProperties</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">triggers</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">parameters</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">options</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">actions</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">description</span>&gt;</span><span class="tag">&lt;/<span class="name">description</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">keepDependencies</span>&gt;</span>false<span class="tag">&lt;/<span class="name">keepDependencies</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">properties</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty</span> <span class="attr">plugin</span>=<span class="string">&quot;gitlab-plugin@1.5.31&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">gitLabConnection</span>&gt;</span><span class="tag">&lt;/<span class="name">gitLabConnection</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">jobCredentialId</span>&gt;</span><span class="tag">&lt;/<span class="name">jobCredentialId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">useAlternativeCredential</span>&gt;</span>false<span class="tag">&lt;/<span class="name">useAlternativeCredential</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">hudson.model.ParametersDefinitionProperty</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">parameterDefinitions</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">net.uaznia.lukanus.hudson.plugins.gitparameter.GitParameterDefinition</span> <span class="attr">plugin</span>=<span class="string">&quot;git-parameter@0.9.16&quot;</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">name</span>&gt;</span>branch_tag<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">uuid</span>&gt;</span>912c32f3-e614-420d-80f3-f8086c4691d6<span class="tag">&lt;/<span class="name">uuid</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">type</span>&gt;</span>PT_BRANCH_TAG<span class="tag">&lt;/<span class="name">type</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">branch</span>&gt;</span><span class="tag">&lt;/<span class="name">branch</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">tagFilter</span>&gt;</span>*<span class="tag">&lt;/<span class="name">tagFilter</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">branchFilter</span>&gt;</span>.*<span class="tag">&lt;/<span class="name">branchFilter</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">sortMode</span>&gt;</span>NONE<span class="tag">&lt;/<span class="name">sortMode</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">defaultValue</span>&gt;</span>origin/master<span class="tag">&lt;/<span class="name">defaultValue</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">selectedValue</span>&gt;</span>NONE<span class="tag">&lt;/<span class="name">selectedValue</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">quickFilterEnabled</span>&gt;</span>false<span class="tag">&lt;/<span class="name">quickFilterEnabled</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">listSize</span>&gt;</span>5<span class="tag">&lt;/<span class="name">listSize</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">requiredParameter</span>&gt;</span>false<span class="tag">&lt;/<span class="name">requiredParameter</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">net.uaznia.lukanus.hudson.plugins.gitparameter.GitParameterDefinition</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">hudson.model.StringParameterDefinition</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">name</span>&gt;</span>port<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">defaultValue</span>&gt;</span>8080<span class="tag">&lt;/<span class="name">defaultValue</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">trim</span>&gt;</span>false<span class="tag">&lt;/<span class="name">trim</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">hudson.model.StringParameterDefinition</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">parameterDefinitions</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">hudson.model.ParametersDefinitionProperty</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">triggers</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">com.dabsquared.gitlabjenkins.GitLabPushTrigger</span> <span class="attr">plugin</span>=<span class="string">&quot;gitlab-plugin@1.5.31&quot;</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">spec</span>&gt;</span><span class="tag">&lt;/<span class="name">spec</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnPush</span>&gt;</span>true<span class="tag">&lt;/<span class="name">triggerOnPush</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerToBranchDeleteRequest</span>&gt;</span>false<span class="tag">&lt;/<span class="name">triggerToBranchDeleteRequest</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnMergeRequest</span>&gt;</span>false<span class="tag">&lt;/<span class="name">triggerOnMergeRequest</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnlyIfNewCommitsPushed</span>&gt;</span>false<span class="tag">&lt;/<span class="name">triggerOnlyIfNewCommitsPushed</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnPipelineEvent</span>&gt;</span>false<span class="tag">&lt;/<span class="name">triggerOnPipelineEvent</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnAcceptedMergeRequest</span>&gt;</span>true<span class="tag">&lt;/<span class="name">triggerOnAcceptedMergeRequest</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnClosedMergeRequest</span>&gt;</span>false<span class="tag">&lt;/<span class="name">triggerOnClosedMergeRequest</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnApprovedMergeRequest</span>&gt;</span>true<span class="tag">&lt;/<span class="name">triggerOnApprovedMergeRequest</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOpenMergeRequestOnPush</span>&gt;</span>never<span class="tag">&lt;/<span class="name">triggerOpenMergeRequestOnPush</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnNoteRequest</span>&gt;</span>true<span class="tag">&lt;/<span class="name">triggerOnNoteRequest</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">noteRegex</span>&gt;</span>Jenkins please retry a build<span class="tag">&lt;/<span class="name">noteRegex</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">ciSkip</span>&gt;</span>true<span class="tag">&lt;/<span class="name">ciSkip</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">skipWorkInProgressMergeRequest</span>&gt;</span>true<span class="tag">&lt;/<span class="name">skipWorkInProgressMergeRequest</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">labelsThatForcesBuildIfAdded</span>&gt;</span><span class="tag">&lt;/<span class="name">labelsThatForcesBuildIfAdded</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">setBuildDescription</span>&gt;</span>true<span class="tag">&lt;/<span class="name">setBuildDescription</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">branchFilterType</span>&gt;</span>All<span class="tag">&lt;/<span class="name">branchFilterType</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">includeBranchesSpec</span>&gt;</span><span class="tag">&lt;/<span class="name">includeBranchesSpec</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">excludeBranchesSpec</span>&gt;</span><span class="tag">&lt;/<span class="name">excludeBranchesSpec</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">sourceBranchRegex</span>&gt;</span><span class="tag">&lt;/<span class="name">sourceBranchRegex</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">targetBranchRegex</span>&gt;</span><span class="tag">&lt;/<span class="name">targetBranchRegex</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">secretToken</span>&gt;</span>&#123;AQAAABAAAAAQGEXb6DKS0zJvyDXcbayiGrgglRQ7E35la+RU6p8KpAc=&#125;<span class="tag">&lt;/<span class="name">secretToken</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">pendingBuildName</span>&gt;</span><span class="tag">&lt;/<span class="name">pendingBuildName</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">cancelPendingBuildsOnUpdate</span>&gt;</span>false<span class="tag">&lt;/<span class="name">cancelPendingBuildsOnUpdate</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">com.dabsquared.gitlabjenkins.GitLabPushTrigger</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">triggers</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">definition</span> <span class="attr">class</span>=<span class="string">&quot;org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition&quot;</span> <span class="attr">plugin</span>=<span class="string">&quot;workflow-cps@2689.v434009a_31b_f1&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-handlebars"><span class="language-xml">pipeline &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    agent any</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    </span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    stages &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        stage(<span class="symbol">&amp;apos;</span>Stage 1: Fetch code from git<span class="symbol">&amp;apos;</span>) &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            steps &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                echo <span class="symbol">&amp;apos;</span>Stage 1: Fetch code from git -- SUCCESS<span class="symbol">&amp;apos;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        stage(<span class="symbol">&amp;apos;</span>Stage 2: Build the project using maven<span class="symbol">&amp;apos;</span>) &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            steps &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                echo <span class="symbol">&amp;apos;</span>Stage 2: Build the project using maven -- SUCCESS<span class="symbol">&amp;apos;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        stage(<span class="symbol">&amp;apos;</span>Stage 3: Make a custom image using docker<span class="symbol">&amp;apos;</span>) &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            steps &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                echo <span class="symbol">&amp;apos;</span>Stage 3: Make a custom image using docker -- SUCCESS<span class="symbol">&amp;apos;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        stage(<span class="symbol">&amp;apos;</span>Stage 4: Push image to Harbor<span class="symbol">&amp;apos;</span>) &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            steps &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                echo <span class="symbol">&amp;apos;</span>Stage 4: Push image to Harbor -- SUCCESS<span class="symbol">&amp;apos;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        stage(<span class="symbol">&amp;apos;</span>Stage 5: Publish over SSH<span class="symbol">&amp;apos;</span>) &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            steps &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                echo <span class="symbol">&amp;apos;</span>Stage 5: Publish over SSH -- SUCCESS<span class="symbol">&amp;apos;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">&#125;</span></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sandbox</span>&gt;</span>true<span class="tag">&lt;/<span class="name">sandbox</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">definition</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">triggers</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">disabled</span>&gt;</span>false<span class="tag">&lt;/<span class="name">disabled</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">flow-definition</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>其实此xml存放于<code>$JENKINS_HOME/jobs/pipeline-test/config.xml</code>，也就是<code>$JENKINS_HOME/jobs/$JOB/config.xml</code>这个文件。</p>
<h4 id="运行代码"><a href="#运行代码" class="headerlink" title="运行代码"></a>运行代码</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">jenkinsServer.createJob(<span class="string">&quot;pipeline2&quot;</span>, xml); <span class="comment">// xml如上</span></span><br></pre></td></tr></table></figure>
<p>抛出异常<code>org.apache.http.client.HttpResponseException: status code: 403, reason phrase: Forbidden</code> 。</p>
<blockquote>
<p>解决方案：<a target="_blank" rel="noopener" href="https://blog.csdn.net/ccc_bigdata/article/details/108080084">https://blog.csdn.net/ccc_bigdata/article/details/108080084</a></p>
</blockquote>
<h1 id="CLI"><a href="#CLI" class="headerlink" title="CLI"></a>CLI</h1><p>下载客户端jar：<a target="_blank" rel="noopener" href="http://211.87.224.233:18080/cli/">http://211.87.224.233:18080/cli/</a></p>
<ul>
<li><p>用法</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar jenkins-cli.jar [-s URL] command [opts...] args...</span><br></pre></td></tr></table></figure>
</li>
<li><p>示例</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar jenkins-cli.jar -s http://211.87.224.233:18080/ -auth admin:admin -webSocket help</span><br></pre></td></tr></table></figure></li>
</ul>
<blockquote>
<p> 参考文档</p>
<p><a target="_blank" rel="noopener" href="http://www.mydlq.club/article/23/">http://www.mydlq.club/article/23/</a></p>
</blockquote>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/05/09/IP%E5%9C%B0%E5%9D%800.0.0.0%E6%98%AF%E4%BB%80%E4%B9%88%E6%84%8F%E6%80%9D%EF%BC%9F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="undefined | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2022/05/09/IP%E5%9C%B0%E5%9D%800.0.0.0%E6%98%AF%E4%BB%80%E4%B9%88%E6%84%8F%E6%80%9D%EF%BC%9F/" class="post-title-link" itemprop="url">IP地址0.0.0.0是什么意思？</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2022-05-09 00:27:15 / 修改时间：00:28:52" itemprop="dateCreated datePublished" datetime="2022-05-09T00:27:15+08:00">2022-05-09</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="IP地址-0-0-0-0-是什么意思"><a href="#IP地址-0-0-0-0-是什么意思" class="headerlink" title="IP地址 0.0.0.0 是什么意思"></a>IP地址 0.0.0.0 是什么意思</h1><h2 id="作用"><a href="#作用" class="headerlink" title="作用"></a>作用</h2><p>IPV4中，0.0.0.0地址被用于表示一个无效的，未知的或者不可用的目标。</p>
<p>在服务器中，0.0.0.0指的是本机上的所有IPV4地址，如果一个主机有两个IP地址，192.168.1.1 和 10.1.2.1，并且该主机上的一个服务监听的地址是0.0.0.0 和端口 8080,那么通过这两个&lt;ip地址:8080&gt;都能够访问该服务。<br>在路由中，0.0.0.0表示的是默认路由，即当路由表中没有找到完全匹配的路由的时候所对应的路由。</p>
<h2 id="用途总结"><a href="#用途总结" class="headerlink" title="用途总结"></a>用途总结</h2><p>当一台主机还没有被分配一个IP地址的时候，用于表示主机本身。（DHCP分配IP地址的时候）<br>用作默认路由，表示”任意IPV4主机”。<br>用来表示目标机器不可用。<br>用作服务端，表示本机上的任意IPV4地址。<br>网关地址 0.0.0.0 表示直连规则，即当前记录对应的 Destination 跟本机在同一个网段，通信时不需要经过网关(路由器)。也就是说使用二层交换机通过MAC即可通信。</p>
<p>命中容器的路由表直连规则，意思是目的IP是在局域网内，不用走到出口网关<br>局域网内直接是通过二层网络来发送包。</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/05/08/%E4%BD%BF%E7%94%A8Docker%E7%9A%84API%E5%8F%8ASDK/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="undefined | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2022/05/08/%E4%BD%BF%E7%94%A8Docker%E7%9A%84API%E5%8F%8ASDK/" class="post-title-link" itemprop="url">使用Docker的API及SDK</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-05-08 02:20:39" itemprop="dateCreated datePublished" datetime="2022-05-08T02:20:39+08:00">2022-05-08</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-06-03 12:46:35" itemprop="dateModified" datetime="2022-06-03T12:46:35+08:00">2022-06-03</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Docker/" itemprop="url" rel="index"><span itemprop="name">Docker</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="Docker的HTTP-API"><a href="#Docker的HTTP-API" class="headerlink" title="Docker的HTTP-API"></a>Docker的HTTP-API</h1><h2 id="开启远程访问端口2375"><a href="#开启远程访问端口2375" class="headerlink" title="开启远程访问端口2375"></a>开启远程访问端口2375</h2><h3 id="修改-x2F-etc-x2F-default-x2F-docker（失败）"><a href="#修改-x2F-etc-x2F-default-x2F-docker（失败）" class="headerlink" title="修改&#x2F;etc&#x2F;default&#x2F;docker（失败）"></a><del>修改&#x2F;etc&#x2F;default&#x2F;docker（失败）</del></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo vim /etc/default/docker</span><br></pre></td></tr></table></figure>

<p>加上</p>
<p>DOCKER_OPTS&#x3D;”-H tcp:&#x2F;&#x2F;0.0.0.0:2375”</p>
<p>再</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo systemctl restart docker</span><br></pre></td></tr></table></figure>





<h3 id="修改-x2F-etc-x2F-docker-x2F-daemon-json（失败）"><a href="#修改-x2F-etc-x2F-docker-x2F-daemon-json（失败）" class="headerlink" title="修改&#x2F;etc&#x2F;docker&#x2F;daemon.json（失败）"></a><del>修改&#x2F;etc&#x2F;docker&#x2F;daemon.json（失败）</del></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/docker/daemon.json</span><br></pre></td></tr></table></figure>

<p>加入</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;hosts&quot;</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">&quot;tcp://0.0.0.0:2375&quot;</span><span class="punctuation">,</span> <span class="string">&quot;unix:///var/run/docker.sock&quot;</span><span class="punctuation">]</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>“unix:&#x2F;&#x2F;&#x2F;var&#x2F;run&#x2F;docker.sock”：unix socket，本地客户端将通过这个来连接 Docker Daemon。</li>
<li>“tcp:&#x2F;&#x2F;0.0.0.0:2375”：tcp socket，表示允许任何远程客户端通过 2375 端口连接 Docker Daemon。</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo systemctl daemon-reload</span><br><span class="line">sudo systemctl restart docker</span><br></pre></td></tr></table></figure>

<p>查看docker进程：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ps -ef | grep docker</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">root      44221      1  1 18:16 ?        00:00:06 /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H <span class="comment">#unix://var/run/docker.sock</span></span></span><br></pre></td></tr></table></figure>
<p>Docker守护进程打开一个HTTP Socket,这样才能实现远程通信</p>
<h3 id="修改-x2F-usr-x2F-lib-x2F-systemd-x2F-system-x2F-docker-service（成功）"><a href="#修改-x2F-usr-x2F-lib-x2F-systemd-x2F-system-x2F-docker-service（成功）" class="headerlink" title="修改&#x2F;usr&#x2F;lib&#x2F;systemd&#x2F;system&#x2F;docker.service（成功）"></a>修改&#x2F;usr&#x2F;lib&#x2F;systemd&#x2F;system&#x2F;docker.service（成功）</h3><p>在&#x2F;usr&#x2F;lib&#x2F;systemd&#x2F;system&#x2F;docker.service，配置远程访问。</p>
<p>主要是在[Service]这个部分，加上两个参数</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">vim /usr/lib/systemd/system/docker.service</span></span><br><span class="line">[Service]</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock</span></span><br><span class="line">ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock</span><br></pre></td></tr></table></figure>

<p>重启</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo systemctl daemon-reload</span><br><span class="line">sudo systemctl restart docker</span><br></pre></td></tr></table></figure>



<p>这次检查我们看到了这个tcp进程:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">root      117236       1  1 02:52 ?        00:00:05 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375 -H unix://var/run/docker.soc</span><br></pre></td></tr></table></figure>



<h2 id="使用Remote-cocker-daemon"><a href="#使用Remote-cocker-daemon" class="headerlink" title="使用Remote cocker daemon"></a>使用Remote cocker daemon</h2><h3 id="shell远程连接"><a href="#shell远程连接" class="headerlink" title="shell远程连接"></a>shell远程连接</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">加上请求主机即可</span></span><br><span class="line">docker -H tcp://211.87.224.233:2375 version</span><br><span class="line">docker -H tcp://ip:port ps</span><br><span class="line">docker -H tcp://ip:port images</span><br></pre></td></tr></table></figure>



<h3 id="RestAPI"><a href="#RestAPI" class="headerlink" title="RestAPI"></a>RestAPI</h3><p>浏览器访问：</p>
<p>比如想要查看版本 <a href="http://ip:port/version">http://ip:port/version</a></p>
<p>其他的操作入口，请参考</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://docs.docker.com/engine/api/v1.41/#tag/Container">Docker API Reference</a></p>
</blockquote>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/hongdada/p/11512901.html">Docker开启Remote API 访问 2375端口 </a></p>
</blockquote>
<h1 id="Docker-SDK-Go"><a href="#Docker-SDK-Go" class="headerlink" title="Docker-SDK-Go"></a>Docker-SDK-Go</h1><h2 id="入门"><a href="#入门" class="headerlink" title="入门"></a>入门</h2><h3 id="构造客户端"><a href="#构造客户端" class="headerlink" title="构造客户端"></a>构造客户端</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">&quot;github.com/docker/docker/client&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	ctx := context.Background()</span><br><span class="line">	<span class="comment">// cli, err := client.NewClientWithOpts(client.FromEnv)</span></span><br><span class="line">	<span class="comment">// remoteDockerDaemonAddress := &quot;211.87.224.233:2375&quot;</span></span><br><span class="line">	<span class="comment">// cli, err := client.NewClientWithOpts(client.FromEnv, client.WithHost(&quot;tcp://&quot; + remoteDockerDaemonAddress))</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>既可以连接本地，也可以连接远程的docker。</p>
<h3 id="登录Registry私服并拉取镜像"><a href="#登录Registry私服并拉取镜像" class="headerlink" title="登录Registry私服并拉取镜像"></a>登录Registry私服并拉取镜像</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">&quot;context&quot;</span></span><br><span class="line">	<span class="string">&quot;github.com/docker/docker/api/types&quot;</span></span><br><span class="line">	<span class="string">&quot;github.com/docker/docker/client&quot;</span></span><br><span class="line">	<span class="string">&quot;io&quot;</span></span><br><span class="line">	<span class="string">&quot;os&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	ctx := context.Background()</span><br><span class="line">	cli, err := client.NewClientWithOpts(client.FromEnv)</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		<span class="built_in">panic</span>(err)</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	harborUrl := <span class="string">&quot;http://211.87.224.233:8930/&quot;</span></span><br><span class="line">	_, err = cli.RegistryLogin(ctx, types.AuthConfig&#123;</span><br><span class="line">		Username:      <span class="string">&quot;admin&quot;</span>,</span><br><span class="line">		Password:      <span class="string">&quot;admin&quot;</span>,</span><br><span class="line">		ServerAddress: harborUrl,</span><br><span class="line">	&#125;)</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		<span class="built_in">panic</span>(<span class="literal">nil</span>)</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	imageName := <span class="string">&quot;211.87.224.233:8930/sdu-weblab/hello-world&quot;</span></span><br><span class="line">  <span class="comment">// out 是响应输出流，可以不输出</span></span><br><span class="line">	out, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions&#123;&#125;)</span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		<span class="built_in">panic</span>(err)</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">defer</span> out.Close()</span><br><span class="line">	io.Copy(os.Stdout, out)</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h3 id="查看容器的详细信息，如端口映射"><a href="#查看容器的详细信息，如端口映射" class="headerlink" title="查看容器的详细信息，如端口映射"></a><del>查看容器的详细信息，如端口映射</del></h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">&quot;context&quot;</span></span><br><span class="line">	<span class="string">&quot;errors&quot;</span></span><br><span class="line">	<span class="string">&quot;fmt&quot;</span></span><br><span class="line">	<span class="string">&quot;github.com/docker/docker/client&quot;</span></span><br><span class="line">	<span class="string">&quot;github.com/docker/go-connections/nat&quot;</span></span><br><span class="line">	<span class="string">&quot;os/exec&quot;</span></span><br><span class="line">	<span class="string">&quot;strings&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="comment">//ip, port, err := getHostIpAndPortOfContainerPort(&quot;8af85dfb4659&quot;, &quot;80&quot;)</span></span><br><span class="line">	ip, port, err := getHostIpAndPortOfContainerPortFromConfig(<span class="string">&quot;8bfb9b8e2692&quot;</span>, <span class="string">&quot;80&quot;</span>)</span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		<span class="built_in">panic</span>(err)</span><br><span class="line">	&#125;</span><br><span class="line">	fmt.Printf(<span class="string">&quot;ip:port = %s:%s&quot;</span>, ip, port)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">getHostIpAndPortOfContainerPortFromConfig</span><span class="params">(containerId <span class="type">string</span>, containerPort <span class="type">string</span>)</span></span> (ip <span class="type">string</span>, port <span class="type">string</span>, err <span class="type">error</span>) &#123;</span><br><span class="line">	cli, err := client.NewClientWithOpts(client.FromEnv)</span><br><span class="line">	containerJSON, err := cli.ContainerInspect(context.Background(), containerId)</span><br><span class="line">	<span class="keyword">if</span> !containerJSON.HostConfig.PublishAllPorts &#123; <span class="comment">// -p hostPort:containerPort</span></span><br><span class="line">		_port, err := nat.NewPort(nat.SplitProtoPort(containerPort))</span><br><span class="line">		<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>, err</span><br><span class="line">		&#125;</span><br><span class="line">		portBindMap := containerJSON.HostConfig.PortBindings</span><br><span class="line">		bindings := portBindMap[_port]</span><br><span class="line">		<span class="keyword">if</span> <span class="built_in">len</span>(bindings) == <span class="number">0</span> &#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>, errors.New(fmt.Sprintf(<span class="string">&quot;%s has no bindings&quot;</span>, containerPort))</span><br><span class="line">		&#125;</span><br><span class="line">		ip = bindings[<span class="number">0</span>].HostIP</span><br><span class="line">		port = bindings[<span class="number">0</span>].HostPort</span><br><span class="line">		<span class="keyword">if</span> <span class="built_in">len</span>(ip) == <span class="number">0</span> &#123;</span><br><span class="line">			ip = <span class="string">&quot;0.0.0.0&quot;</span></span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> ip, port, <span class="literal">nil</span></span><br><span class="line">	&#125; <span class="keyword">else</span> &#123; <span class="comment">// -P containerPort</span></span><br><span class="line">		<span class="keyword">return</span> getHostIpAndPortOfContainerPort(containerId, containerPort)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// todo </span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">getHostIpAndPortOfContainerPort</span><span class="params">(containerId <span class="type">string</span>, containerPort <span class="type">string</span>)</span></span> (ip <span class="type">string</span>, port <span class="type">string</span>, err <span class="type">error</span>) &#123;</span><br><span class="line">	shell := <span class="string">&quot;docker port &quot;</span> + containerId + <span class="string">&quot; | grep &quot;</span> + containerPort + <span class="string">&quot; | awk &#x27;&#123;print $3&#125;&#x27;&quot;</span></span><br><span class="line">	fmt.Println(shell)</span><br><span class="line">	output, err := exec.Command(<span class="string">&quot;bash&quot;</span>, <span class="string">&quot;-c&quot;</span>, shell).Output()</span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		<span class="keyword">return</span></span><br><span class="line">	&#125;</span><br><span class="line">	ipPort := <span class="type">string</span>(output)</span><br><span class="line">	<span class="comment">//fmt.Println(ipPort)</span></span><br><span class="line">	splits := strings.Split(ipPort, <span class="string">&quot;:&quot;</span>)</span><br><span class="line">	<span class="keyword">if</span> <span class="built_in">len</span>(splits) != <span class="number">2</span> &#123;</span><br><span class="line">		err = errors.New(<span class="string">&quot;invalid format&quot;</span>)</span><br><span class="line">		<span class="keyword">return</span></span><br><span class="line">	&#125;</span><br><span class="line">	ip = splits[<span class="number">0</span>]</span><br><span class="line">	port = splits[<span class="number">1</span>]</span><br><span class="line">	<span class="keyword">return</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>如果端口是用<code>-P</code>的方式随机映射的话就不能用<code>ContainerInspect</code>这个API了。</p>
<h3 id="获取端口映射（获取其一即可）"><a href="#获取端口映射（获取其一即可）" class="headerlink" title="获取端口映射（获取其一即可）"></a>获取端口映射（获取其一即可）</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">getHostPortOfContainerPort</span><span class="params">(containerId <span class="type">string</span>, containerPort <span class="type">string</span>)</span></span> (port <span class="type">string</span>, err <span class="type">error</span>) &#123;</span><br><span class="line">	shell := <span class="string">&quot;docker port &quot;</span> + containerId + <span class="string">&quot; | grep &quot;</span> + containerPort + <span class="string">&quot; | awk &#x27;&#123;print $3&#125;&#x27;&quot;</span></span><br><span class="line">	output, err := exec.Command(<span class="string">&quot;bash&quot;</span>, <span class="string">&quot;-c&quot;</span>, shell).Output()</span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		<span class="keyword">return</span></span><br><span class="line">	&#125;</span><br><span class="line">	shellOut := <span class="type">string</span>(output)</span><br><span class="line">	shellOut = <span class="string">&quot;0.0.0.0:49155\n:::49155&quot;</span></span><br><span class="line">	parts := strings.Split(shellOut, <span class="string">&quot;\n&quot;</span>)</span><br><span class="line">	<span class="keyword">if</span> <span class="built_in">len</span>(parts) == <span class="number">0</span> &#123;</span><br><span class="line">		err = fmt.Errorf(<span class="string">&quot;containerPort=%s has no bindings&quot;</span>, containerPort)</span><br><span class="line">		<span class="keyword">return</span></span><br><span class="line">	&#125;</span><br><span class="line">	ipPort := parts[<span class="number">0</span>]</span><br><span class="line">	splits := strings.Split(ipPort, <span class="string">&quot;:&quot;</span>)</span><br><span class="line">	<span class="keyword">if</span> <span class="built_in">len</span>(splits) != <span class="number">2</span> &#123;</span><br><span class="line">		err = errors.New(<span class="string">&quot;invalid format&quot;</span>)</span><br><span class="line">		<span class="keyword">return</span></span><br><span class="line">	&#125;</span><br><span class="line">	port = splits[<span class="number">1</span>]</span><br><span class="line">	<span class="keyword">return</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>






<h3 id="创建容器"><a href="#创建容器" class="headerlink" title="创建容器"></a>创建容器</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> (</span><br><span class="line">    imageName     <span class="type">string</span>   = <span class="string">&quot;my-gin:latest&quot;</span>                      <span class="comment">//镜像名称</span></span><br><span class="line">    containerName <span class="type">string</span>   = <span class="string">&quot;mygin-latest&quot;</span>                       <span class="comment">//容器名称</span></span><br><span class="line">    indexName     <span class="type">string</span>   = <span class="string">&quot;/&quot;</span> + containerName                  <span class="comment">//容器索引名称，用于检查该容器是否存在是使用</span></span><br><span class="line">    cmd           <span class="type">string</span>   = <span class="string">&quot;./ginDocker2&quot;</span>                       <span class="comment">//运行的cmd命令，用于启动container中的程序</span></span><br><span class="line">    workDir       <span class="type">string</span>   = <span class="string">&quot;/go/src/ginDocker2&quot;</span>                 <span class="comment">//container工作目录</span></span><br><span class="line">    openPort      nat.Port = <span class="string">&quot;7070&quot;</span>                               <span class="comment">//container开放端口</span></span><br><span class="line">    hostPort      <span class="type">string</span>   = <span class="string">&quot;7070&quot;</span>                               <span class="comment">//container映射到宿主机的端口</span></span><br><span class="line">    containerDir  <span class="type">string</span>   = <span class="string">&quot;/go/src/ginDocker2&quot;</span>                 <span class="comment">//容器挂载目录</span></span><br><span class="line">    hostDir       <span class="type">string</span>   = <span class="string">&quot;/home/youngblood/Go/src/ginDocker2&quot;</span> <span class="comment">//容器挂在载宿主机的目录</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//创建容器</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">createContainer</span><span class="params">(ctx context.Context, cli *client.Client)</span></span> &#123;</span><br><span class="line">    <span class="comment">//创建容器</span></span><br><span class="line">    cont, err := cli.ContainerCreate(ctx, &amp;container.Config&#123;</span><br><span class="line">        Image:      imageName,     <span class="comment">//镜像名称</span></span><br><span class="line">        Tty:        <span class="literal">true</span>,          <span class="comment">//docker run命令中的-t选项</span></span><br><span class="line">        OpenStdin:  <span class="literal">true</span>,          <span class="comment">//docker run命令中的-i选项</span></span><br><span class="line">        Cmd:        []<span class="type">string</span>&#123;cmd&#125;, <span class="comment">//docker 容器中执行的命令</span></span><br><span class="line">        WorkingDir: workDir,       <span class="comment">//docker容器中的工作目录</span></span><br><span class="line">        ExposedPorts: nat.PortSet&#123;</span><br><span class="line">            openPort: <span class="keyword">struct</span>&#123;&#125;&#123;&#125;, <span class="comment">//docker容器对外开放的端口</span></span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;, &amp;container.HostConfig&#123;</span><br><span class="line">        PortBindings: nat.PortMap&#123;</span><br><span class="line">            <span class="comment">// 一个容器端口可以”一对多“地映射到多个宿主机端口</span></span><br><span class="line">            openPort: []nat.PortBinding&#123;nat.PortBinding&#123;</span><br><span class="line">                HostIP:   <span class="string">&quot;0.0.0.0&quot;</span>, <span class="comment">//docker容器映射的宿主机的ip, 0.0.0.0 表示服务器的所有可监听的ip</span></span><br><span class="line">                HostPort: hostPort,  <span class="comment">//docker 容器映射到宿主机的端口;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        Mounts: []mount.Mount&#123; <span class="comment">//docker 容器目录挂在到宿主机目录</span></span><br><span class="line">            mount.Mount&#123;</span><br><span class="line">                Type:   mount.TypeBind,</span><br><span class="line">                Source: hostDir,</span><br><span class="line">                Target: containerDir,</span><br><span class="line">            &#125;,</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;, <span class="literal">nil</span>, containerName)</span><br><span class="line">    <span class="keyword">if</span> err == <span class="literal">nil</span> &#123;</span><br><span class="line">        log.Printf(<span class="string">&quot;success create container:%s\n&quot;</span>, cont.ID)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        log.Println(<span class="string">&quot;failed to create container!!!!!!!!!!!!!&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果要实现-P（随机映射端口的话）的话：使用</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&amp;container.HostConfig&#123;</span><br><span class="line">   PublishAllPorts: <span class="literal">true</span>,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<p><strong>举例</strong></p>
<p>将<code>docker run -d -p 9000:80 --name docker.getting-started.latest docker/getting-started:latest</code>改写为docker程序：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">&quot;context&quot;</span></span><br><span class="line">	<span class="string">&quot;fmt&quot;</span></span><br><span class="line">	<span class="string">&quot;github.com/docker/docker/api/types&quot;</span></span><br><span class="line">	<span class="string">&quot;github.com/docker/docker/api/types/container&quot;</span></span><br><span class="line">	<span class="string">&quot;github.com/docker/docker/client&quot;</span></span><br><span class="line">	<span class="string">&quot;github.com/docker/go-connections/nat&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	ctx := context.Background()</span><br><span class="line">	cli, err := client.NewClientWithOpts(client.FromEnv)</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		<span class="built_in">panic</span>(err)</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	imageName := <span class="string">&quot;docker/getting-started:latest&quot;</span></span><br><span class="line">	containerName := <span class="string">&quot;docker.getting-started.latest&quot;</span></span><br><span class="line">	containerPort := nat.Port(<span class="string">&quot;80&quot;</span>)</span><br><span class="line">	hostPort := <span class="string">&quot;9000&quot;</span></span><br><span class="line">	resp, err := cli.ContainerCreate(ctx,</span><br><span class="line">		&amp;container.Config&#123;</span><br><span class="line">			Image: imageName,</span><br><span class="line">			ExposedPorts: nat.PortSet&#123;</span><br><span class="line">				containerPort: <span class="keyword">struct</span>&#123;&#125;&#123;&#125;,</span><br><span class="line">			&#125;,</span><br><span class="line">		&#125;, &amp;container.HostConfig&#123;</span><br><span class="line">			PortBindings: nat.PortMap&#123;</span><br><span class="line">				containerPort: []nat.PortBinding&#123; <span class="comment">//</span></span><br><span class="line">					&#123;</span><br><span class="line">						HostIP:   <span class="string">&quot;0.0.0.0&quot;</span>,</span><br><span class="line">						HostPort: hostPort,</span><br><span class="line">					&#125;,</span><br><span class="line">				&#125;,</span><br><span class="line">			&#125;,</span><br><span class="line">		&#125;, <span class="literal">nil</span>, <span class="literal">nil</span>, containerName)</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		<span class="built_in">panic</span>(err)</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions&#123;&#125;); err != <span class="literal">nil</span> &#123;</span><br><span class="line">		<span class="built_in">panic</span>(err)</span><br><span class="line">	&#125;</span><br><span class="line">	fmt.Printf(<span class="string">&quot;containerID = %s\n&quot;</span>, resp.ID)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="读取日志"><a href="#读取日志" class="headerlink" title="读取日志"></a>读取日志</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">   <span class="string">&quot;context&quot;</span></span><br><span class="line">   <span class="string">&quot;io&quot;</span></span><br><span class="line">   <span class="string">&quot;os&quot;</span></span><br><span class="line"></span><br><span class="line">   <span class="string">&quot;github.com/docker/docker/api/types&quot;</span></span><br><span class="line">   <span class="string">&quot;github.com/docker/docker/client&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">   ctx := context.Background()</span><br><span class="line">   cli, err := client.NewClientWithOpts(client.FromEnv)</span><br><span class="line">   <span class="comment">//remoteDockerDaemonAddress := &quot;211.87.224.233:2375&quot;</span></span><br><span class="line">   <span class="comment">//cli, err := client.NewClientWithOpts(client.FromEnv, client.WithHost(&quot;tcp://&quot;+remoteDockerDaemonAddress))</span></span><br><span class="line">   <span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">      <span class="built_in">panic</span>(err)</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   options := types.ContainerLogsOptions&#123;</span><br><span class="line">      ShowStdout: <span class="literal">true</span>,</span><br><span class="line">      ShowStderr: <span class="literal">true</span>,</span><br><span class="line">      Follow:     <span class="literal">true</span>,</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="comment">// Replace this ID with a container that really exists</span></span><br><span class="line">   out, err := cli.ContainerLogs(ctx, <span class="string">&quot;87f42c0ab7f5&quot;</span>, options)</span><br><span class="line">   <span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">      <span class="built_in">panic</span>(err)</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   io.Copy(os.Stdout, out)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>如果将日志实时输出到websocket呢？如下：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">&quot;bufio&quot;</span></span><br><span class="line">	<span class="string">&quot;context&quot;</span></span><br><span class="line">	<span class="string">&quot;io&quot;</span></span><br><span class="line">	<span class="string">&quot;log&quot;</span></span><br><span class="line">	<span class="string">&quot;net/http&quot;</span></span><br><span class="line">	<span class="string">&quot;os&quot;</span></span><br><span class="line"></span><br><span class="line">	<span class="string">&quot;github.com/docker/docker/api/types&quot;</span></span><br><span class="line">	<span class="string">&quot;github.com/docker/docker/client&quot;</span></span><br><span class="line">	<span class="string">&quot;github.com/gorilla/websocket&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> DefaultBufferSize = <span class="number">4</span> * <span class="number">1024</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> (</span><br><span class="line">	out      io.ReadCloser</span><br><span class="line">	upgrader = websocket.Upgrader&#123;</span><br><span class="line">		CheckOrigin: <span class="function"><span class="keyword">func</span><span class="params">(r *http.Request)</span></span> <span class="type">bool</span> &#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">		&#125;,</span><br><span class="line">	&#125; <span class="comment">// use default options</span></span><br><span class="line">	conn *websocket.Conn</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	ctx := context.Background()</span><br><span class="line">	<span class="comment">//cli, err := client.NewClientWithOpts(client.FromEnv)</span></span><br><span class="line">	remoteDockerDaemonAddress := <span class="string">&quot;211.87.224.233:2375&quot;</span></span><br><span class="line">	cli, err := client.NewClientWithOpts(client.FromEnv, client.WithHost(<span class="string">&quot;tcp://&quot;</span>+remoteDockerDaemonAddress))</span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		<span class="built_in">panic</span>(err)</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	options := types.ContainerLogsOptions&#123;</span><br><span class="line">		ShowStdout: <span class="literal">true</span>,</span><br><span class="line">		ShowStderr: <span class="literal">true</span>,</span><br><span class="line">		Follow:     <span class="literal">true</span>,</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// Replace this ID with a container that really exists</span></span><br><span class="line">	<span class="comment">//out, err = cli.ContainerLogs(ctx, &quot;8bfb9b8e2692&quot;, options) // local</span></span><br><span class="line">	out, err = cli.ContainerLogs(ctx, <span class="string">&quot;87f42c0ab7f5&quot;</span>, options) <span class="comment">// remote</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		<span class="built_in">panic</span>(err)</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	http.HandleFunc(<span class="string">&quot;/logs&quot;</span>, socketHandler)</span><br><span class="line">	log.Fatal(http.ListenAndServe(<span class="string">&quot;localhost:8081&quot;</span>, <span class="literal">nil</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">socketHandler</span><span class="params">(w http.ResponseWriter, r *http.Request)</span></span> &#123;</span><br><span class="line">	<span class="comment">// Upgrade our raw HTTP connection to a websocket based one</span></span><br><span class="line">	_conn, err := upgrader.Upgrade(w, r, <span class="literal">nil</span>)</span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		log.Print(<span class="string">&quot;Error during connection upgradation:&quot;</span>, err)</span><br><span class="line">		os.Exit(<span class="number">3</span>)</span><br><span class="line">	&#125;</span><br><span class="line">	conn = _conn</span><br><span class="line">	<span class="keyword">defer</span> conn.Close()</span><br><span class="line"></span><br><span class="line">	reader := bufio.NewReader(out)</span><br><span class="line">	<span class="comment">// The event loop</span></span><br><span class="line">	<span class="keyword">for</span> &#123;</span><br><span class="line">		message, err := reader.ReadString(<span class="string">&#x27;\n&#x27;</span>)</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">			<span class="keyword">if</span> err == io.EOF &#123;</span><br><span class="line">				exitAndNotifyPeer(message, <span class="number">0</span>)</span><br><span class="line">			&#125;</span><br><span class="line">			exitAndNotifyPeer(<span class="string">&quot;\n&quot;</span>, <span class="number">1</span>)</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">// todo delete</span></span><br><span class="line">		log.Printf(<span class="string">&quot;Log read is: %s&quot;</span>, message)</span><br><span class="line"></span><br><span class="line">		err = conn.WriteMessage(websocket.TextMessage, []<span class="type">byte</span>(message))</span><br><span class="line">		<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">			log.Println(<span class="string">&quot;Error during message writing: &quot;</span>, err)</span><br><span class="line">			os.Exit(<span class="number">2</span>)</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">exitAndNotifyPeer</span><span class="params">(msg <span class="type">string</span>, exitCode <span class="type">int</span>)</span></span> &#123;</span><br><span class="line">	conn.WriteMessage(websocket.CloseMessage, []<span class="type">byte</span>(msg))</span><br><span class="line">	os.Exit(exitCode)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>






<blockquote>
<p>参考文档</p>
<p><a target="_blank" rel="noopener" href="https://docs.docker.com/engine/api/sdk/examples/">一些小例子</a></p>
<p><a target="_blank" rel="noopener" href="https://pkg.go.dev/github.com/docker/docker/client">Reference</a></p>
</blockquote>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/05/08/Git%E7%9A%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="undefined | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2022/05/08/Git%E7%9A%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="post-title-link" itemprop="url">Git的学习笔记</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-05-08 01:43:28" itemprop="dateCreated datePublished" datetime="2022-05-08T01:43:28+08:00">2022-05-08</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-10-18 15:52:14" itemprop="dateModified" datetime="2022-10-18T15:52:14+08:00">2022-10-18</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/DevOps/" itemprop="url" rel="index"><span itemprop="name">DevOps</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="安装与配置"><a href="#安装与配置" class="headerlink" title="安装与配置"></a>安装与配置</h1><p>Mac上使用图形化安装工具、brew、xcode自带的都可以。<br>检查</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git --version</span><br></pre></td></tr></table></figure>

<h2 id="环境配置"><a href="#环境配置" class="headerlink" title="环境配置"></a>环境配置</h2><p>配置文件为 *gitconfig</p>
<p>作用域有三种：</p>
<ol>
<li>系统 system (&#x2F;etc&#x2F;gitconfig)</li>
<li>全局（用户）global (在 ~&#x2F;.gitconfig )</li>
<li>项目 (在仓库的 &#x2F;project&#x2F;.git&#x2F;config)</li>
</ol>
<ul>
<li><p>查看</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git config --list</span><br></pre></td></tr></table></figure>
</li>
<li><p>配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git config --global user.name <span class="string">&#x27;jsy&#x27;</span></span><br><span class="line">git config --global user.email <span class="string">&#x27;153xxxxx@qq.com&#x27;</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h1 id="基本概念"><a href="#基本概念" class="headerlink" title="基本概念"></a>基本概念</h1><h2 id="版本库、工作区、暂存区"><a href="#版本库、工作区、暂存区" class="headerlink" title="版本库、工作区、暂存区"></a>版本库、工作区、暂存区</h2><ul>
<li><p>版本库<br>就是那个 .git<br>里面有</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">jisongyang@SongyangJi-MacBookAir .git % <span class="built_in">ls</span></span><br><span class="line">HEAD		description	info		refs</span><br><span class="line">config		hooks		objects</span><br></pre></td></tr></table></figure>

</li>
<li><p>工作区<br>我们能看到的那个包含.git的文件夹, 比如下面的那个 repo 文件夹就是</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">jisongyang@SongyangJi-MacBookAir .git % <span class="built_in">cd</span> ..</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % <span class="built_in">pwd</span></span><br><span class="line">/Users/jisongyang/learn-git-repo/repo</span><br></pre></td></tr></table></figure>
</li>
<li><p>暂存区<br>临时保存的修改的文件的地方</p>
</li>
</ul>
<h2 id="git下文件的状态"><a href="#git下文件的状态" class="headerlink" title="git下文件的状态"></a>git下文件的状态</h2><ol>
<li>untracked 未被git管理</li>
<li>tracked<ol>
<li>unmodified 未修改</li>
<li>modified 已修改</li>
<li>staged 暂存</li>
</ol>
</li>
</ol>
<p>查看状态</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git status</span><br><span class="line"><span class="comment"># 未跟踪</span></span><br><span class="line">git status -s</span><br></pre></td></tr></table></figure>

<p>举例</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">jisongyang@SongyangJi-MacBookAir repo % clear</span><br><span class="line"><span class="comment"># 未跟踪</span></span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git status -s</span><br><span class="line">?? a.txt</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git add a.txt</span><br><span class="line"><span class="comment"># 已跟踪</span></span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git status -s</span><br><span class="line">A  a.txt</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % <span class="built_in">echo</span> hello &gt;&gt; a.txt</span><br><span class="line"><span class="comment"># 已修改</span></span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git status -s</span><br><span class="line">AM a.txt</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git reset a.txt</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git status -s</span><br><span class="line">?? a.txt</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git commit a.txt</span><br><span class="line">[master (root-commit) 5273424] init a.txt</span><br><span class="line"> 1 file changed, 2 insertions(+)</span><br><span class="line"> create mode 100644 a.txt</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git status -s</span><br><span class="line"><span class="comment"># a.txt 在状态里不可见了</span></span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo %</span><br></pre></td></tr></table></figure>

<h2 id="gitignore"><a href="#gitignore" class="headerlink" title=".gitignore"></a>.gitignore</h2><p>当有文件不需要git管理的时候，使用这个文件就很有必要了。<br>比如日志、项目编译出来的文件、IDEA的本地信息等等，就不需要git管理。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 文件名固定</span></span><br><span class="line"><span class="built_in">touch</span> .gitignore</span><br></pre></td></tr></table></figure>

<p>看例子就好</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 忽略所有 *.class</span></span><br><span class="line">*.class</span><br><span class="line"><span class="comment"># 但是不忽略Hello.class</span></span><br><span class="line">!Hello.class</span><br><span class="line"><span class="comment"># 忽略当前目录下的TODO文件夹</span></span><br><span class="line">/TODO </span><br><span class="line"><span class="comment"># 忽略 target 下的所有文件</span></span><br><span class="line">target/</span><br><span class="line"><span class="comment"># 忽略 doc/*.txt的所有文件夹</span></span><br><span class="line">doc/*.txt</span><br><span class="line"><span class="comment">## 忽略 doc及其所有子目录下所有的 *.txt</span></span><br><span class="line">doc/**/*.txt</span><br></pre></td></tr></table></figure>


<h2 id="仓库"><a href="#仓库" class="headerlink" title="仓库"></a>仓库</h2><ul>
<li>在本地初始化仓库</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git init</span><br></pre></td></tr></table></figure>
<p>创建完会多一个 .init文件(默认不可见)</p>
<ul>
<li>从远程仓库克隆<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">clone</span> repo</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="暂存区"><a href="#暂存区" class="headerlink" title="暂存区"></a>暂存区</h2><p>从工作区到暂存区</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git add filename</span><br><span class="line"><span class="comment"># 添加全部文件</span></span><br><span class="line">git add .</span><br></pre></td></tr></table></figure>
<p>暂存区的目录树会被重写，被 master 分支指向的目录树所替换，但是工作区不受影响</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git reset HEAD</span><br></pre></td></tr></table></figure>

<p>直接从暂存区删除文件，工作区则不做出改变</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">rm</span> --cached -f &lt;file&gt;</span><br></pre></td></tr></table></figure>


<h2 id="版本库"><a href="#版本库" class="headerlink" title="版本库"></a>版本库</h2><ul>
<li><p>从暂存区到版本库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -m <span class="string">&#x27;message&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>修改上一次的commit</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit --amend</span><br></pre></td></tr></table></figure>
<p>具体使用方法见<a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/100243017">git commit –amend 修改git提交记录用法详解</a></p>
</li>
</ul>
<h1 id="远程仓库"><a href="#远程仓库" class="headerlink" title="远程仓库"></a>远程仓库</h1><p><strong>查看已连接的远程仓库</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git remote</span><br><span class="line">git remote -v</span><br><span class="line">git remote show origin</span><br></pre></td></tr></table></figure>

<p><strong>添加远程仓库</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote add [shortname] url</span><br></pre></td></tr></table></figure>

<p><strong>从远程仓库克隆到本地</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">clone</span> repo</span><br></pre></td></tr></table></figure>

<p><strong>移除无效的远程仓库</strong>（只是移除关联关系，不会对远程仓库有影响）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote <span class="built_in">rm</span> origin</span><br></pre></td></tr></table></figure>

<p><strong>抓取(fetch)</strong><br>从远程仓库将最新版本获取到本地仓库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git fetch</span><br></pre></td></tr></table></figure>

<p><strong>拉取(pull)</strong><br>相当于fetch + merge : 从远程仓库将最新版本获取到本地仓库，并merge<br>（根据配置也可能是相当于 fetch + rebase）<br>(可能报 merge unrelated histories)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git pull [romote-name] [branch-name]</span><br></pre></td></tr></table></figure>

<p><strong>推送(push)</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push [romote-name] [branch-name]</span><br></pre></td></tr></table></figure>



<h1 id="分支"><a href="#分支" class="headerlink" title="分支"></a>分支</h1><p>master分支是git自动创建的，但没并没有什么特殊之处。</p>
<p><strong>查看分支</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 列出所有本地分支</span></span><br><span class="line">git branch</span><br><span class="line"><span class="comment"># 列出所有远程分支</span></span><br><span class="line">git branch -r</span><br><span class="line"><span class="comment"># 列出远程和本地分支</span></span><br><span class="line">git branch -a</span><br></pre></td></tr></table></figure>

<p><strong>创建分支</strong>(在原先分支下创建分支，内容会初始共享)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch branch-name</span><br></pre></td></tr></table></figure>
<p>举例（*master表示当前在master分支下）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">jisongyang@SongyangJi-MacBookAir repo1 % git branch b1</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo1 % git branch</span><br><span class="line">  b1</span><br><span class="line">* master</span><br></pre></td></tr></table></figure>


<p><strong>切换分支</strong>（这个时候就自动把目标分支在本地仓库里的文件换到工作区里）</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">git checkout b1</span><br><span class="line"></span><br><span class="line"># &quot;打开&quot;远程仓库的分支</span><br><span class="line"># 如果没有全部拉下来的话</span><br><span class="line"># git fetch -all</span><br><span class="line">git checkout -b dev（本地分支名） origin/dev（远程分支名）</span><br></pre></td></tr></table></figure>

<p>两个命令合并到一步：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b dev</span><br></pre></td></tr></table></figure>


<p><strong>将本地分支推送至远程仓库</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push -u origin b1</span><br></pre></td></tr></table></figure>

<p><strong>分支合并</strong><br>(可能会冲突)<br>这里的冲突需要正确理解（这里的冲突的就像是乐观锁的加版本号一样，事实上在svn里确实有全局版本号，不允许脏写发生）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 把b1分支合并到当前的master分支</span></span><br><span class="line">git merge b1</span><br></pre></td></tr></table></figure>

<p><strong>rebase</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git fetch</span><br><span class="line">git rebase dev</span><br></pre></td></tr></table></figure>

<p><strong>是rebase还是merge？</strong><br>git rebase和git merge这两个命令都旨在将更改代码从一个分支合并到另一个分支，但二者的合并方式却有很大的不同。<br>git merge优点是分支代码合并后不破坏原分支的代码提交记录，缺点就是会产生额外的提交记录并进行两条分支的合并，<br>git rebase 优点是无须新增提交记录到目标分支，rebase后可以将对象分支的提交历史续上目标分支上，形成线性提交历史记录，进行review的时候更加直观。</p>
<blockquote>
<p><strong>git rebase的黄金原则</strong><br><strong>不能对一个共享的分支进行Git rebase操作</strong>。<br><del>比如，现在在feature分支上，现在想要合并master分支的代码：<br>也就是说，如果master分支是共享的（这是一种很常见的方式），<br>那么就不能进行 git rebase master，<br>而是要进行 git merge master。</del> </p>
</blockquote>
<p>总结</p>
<ul>
<li><strong>融合代码到公共分支的时使用git merge</strong>，而不用git rebase</li>
<li><strong>融合代码到个人分支的时候使用git rebase</strong>，可以不污染分支的提交记录，形成简洁的线性提交历史记录。<br>参考 <a target="_blank" rel="noopener" href="https://joyohub.com/2020/04/06/git-rebase/">git rebase和git merge有什么区别？</a></li>
</ul>
<p><strong>删除分支</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 删除的是本地的分支，对远程的分支无关</span></span><br><span class="line">git branch -d branch-name</span><br><span class="line"><span class="comment"># 强制删除（git的保护，以防误删）</span></span><br><span class="line">git branch -D branch-name</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">jisongyang@SongyangJi-MacBookAir repo1 % git branch -d b1</span><br><span class="line">error: The branch <span class="string">&#x27;b1&#x27;</span> is not fully merged.</span><br><span class="line">If you are sure you want to delete it, run <span class="string">&#x27;git branch -D b1&#x27;</span>.</span><br></pre></td></tr></table></figure>

<p><strong>删除远程分支</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin -d branchname</span><br></pre></td></tr></table></figure>




<h1 id="标签"><a href="#标签" class="headerlink" title="标签"></a>标签</h1><p>Git给历史中的某次提交打上标签，以示重要。<br>标签指的是某个分支某个特定时间节点的状态。（就像是打了一份快照）</p>
<p><strong>查看已有标签</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 列出已有标签</span></span><br><span class="line">git tag</span><br><span class="line">git show tag</span><br></pre></td></tr></table></figure>
<p><strong>创建标签</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">git tag tagname</span><br><span class="line">git tag tagname -m message</span><br><span class="line"><span class="comment"># 举例</span></span><br><span class="line">git tag v1.0 -m <span class="string">&#x27;finally a stable release&#x27;</span></span><br></pre></td></tr></table></figure>

<p><strong>将标签推送到远程仓库</strong>：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 推送某个标签</span></span><br><span class="line">git push origin vxx</span><br><span class="line"><span class="comment"># 推送所有不在remote的标签</span></span><br><span class="line">git push origin --tags</span><br></pre></td></tr></table></figure>

<p>实操</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 拉取所有版本</span></span><br><span class="line">git fetch --all --tags</span><br><span class="line"><span class="comment"># 查看有哪些版本</span></span><br><span class="line">git tag</span><br><span class="line"><span class="comment"># 创建某个版本的新的分支并切换过去</span></span><br><span class="line">git checkout tags/v1.0 -b v1.0-branch</span><br></pre></td></tr></table></figure>


<p><a target="_blank" rel="noopener" href="https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E6%89%93%E6%A0%87%E7%AD%BE">Git-基础-打标签</a></p>
<h1 id="命令拾遗"><a href="#命令拾遗" class="headerlink" title="命令拾遗"></a>命令拾遗</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 查看工作区和暂存区的文件</span></span><br><span class="line">git ls-files</span><br></pre></td></tr></table></figure>

<p>删除</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">rm</span> file</span><br></pre></td></tr></table></figure>
<p><code>git rm</code>和<code>rm</code>的区别很多，网上讲的也都是其中的一个方面，不细讲了，最好自己试试。</p>
<p>查看日志</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">log</span></span><br></pre></td></tr></table></figure>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




  <nav class="pagination">
    <a class="extend prev" rel="prev" title="上一页" aria-label="上一页" href="/page/9/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/9/">9</a><span class="page-number current">10</span><a class="page-number" href="/page/11/">11</a><span class="space">&hellip;</span><a class="page-number" href="/page/26/">26</a><a class="extend next" rel="next" title="下一页" aria-label="下一页" href="/page/11/"><i class="fa fa-angle-right"></i></a>
  </nav>

</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">SongyangJi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  





</body>
</html>
